package com.emip.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.AlarmConfigBean;
import com.emip.bean.DeviceBean;
import com.emip.bean.MonitorTypeBean;
import com.emip.dao.DeviceDAO;
import com.emip.service.DeviceService;

@Service("deviceService")
public class DeviceServiceImpl implements DeviceService {

	@Autowired
	DeviceDAO deviceDAO;
	
	public DeviceBean addDevice(MonitorTypeBean monitorType, String deviceType,
			String devicename, String deviceDisplayName,
			String instructionCode, String deviceImageUrl, int dWordSeq,
			int bitSeq, double statusValue, String statDesc, int controllFlag,
			String statImageUrl, AlarmConfigBean alarmConfig,
			String alarmContent, String remark) {
		DeviceBean newBean = new DeviceBean();
		newBean.setMonitorType(monitorType);
		newBean.setDeviceType(deviceType);
		newBean.setDevicename(devicename);
		newBean.setDeviceDisplayName(deviceDisplayName);
		newBean.setInstructionCode(instructionCode);
		newBean.setDeviceImageUrl(deviceImageUrl);
		newBean.setdWordSeq(dWordSeq);
		newBean.setBitSeq(bitSeq);
		newBean.setStatusValue(statusValue);
		newBean.setStatDesc(statDesc);
		newBean.setControllFlag(controllFlag);
		newBean.setStatImageUrl(statImageUrl);
		newBean.setAlarmConfig(alarmConfig);
		newBean.setAlarmContent(alarmContent);
		newBean.setRemark(remark);
		//查重，是否已经有相同的monitorType, devicename的记录存在
		//若有重复记录，则返回null
		if (deviceDAO.findExist(newBean)) {
			return null;
		}
		int newID = deviceDAO.add(newBean);
		newBean.setId(newID);
		return newBean;
	}

	public Boolean deleteDevice(int id) {
		DeviceBean delBean = deviceDAO.findByID(id);
		deviceDAO.remove(delBean);
		return true;
	}

	public List<DeviceBean> getAll() {
		return deviceDAO.getAll();
	}

	public DeviceBean getDeviceByID(int id) {
		return deviceDAO.findByID(id);
	}

	public DeviceBean updateDevice(int id, MonitorTypeBean monitorType,
			String deviceType, String devicename, String deviceDisplayName,
			String instructionCode, String deviceImageUrl, int dWordSeq,
			int bitSeq, double statusValue, String statDesc, int controllFlag,
			String statImageUrl, AlarmConfigBean alarmConfig,
			String alarmContent, String remark) {
		DeviceBean newBean = deviceDAO.findByID(id);
		newBean.setMonitorType(monitorType);
		newBean.setDeviceType(deviceType);
		newBean.setDevicename(devicename);
		newBean.setDeviceDisplayName(deviceDisplayName);
		newBean.setInstructionCode(instructionCode);
		newBean.setDeviceImageUrl(deviceImageUrl);
		newBean.setdWordSeq(dWordSeq);
		newBean.setBitSeq(bitSeq);
		newBean.setStatusValue(statusValue);
		newBean.setStatDesc(statDesc);
		newBean.setControllFlag(controllFlag);
		newBean.setStatImageUrl(statImageUrl);
		newBean.setAlarmConfig(alarmConfig);
		newBean.setAlarmContent(alarmContent);
		newBean.setRemark(remark);
		//查重，是否已经有相同的monitorType, devicename的记录存在
		//若有重复记录，则返回null
		if (deviceDAO.findExist(newBean)) {
			return null;
		}
		deviceDAO.update(newBean);
		return newBean;
	}

}
